var Performance = {
    id: "CustomerPerformanceTable",
    seItem: null,
    table: null,
    layerIndex: -1
};

Performance.initColumn = function () {
    var columns = [
        {field: 'selectItem', radio: true},
        {title: 'id', field: 'id', visible: false, align: 'center', valign: 'middle'},
        {title: 'reservationsId', field: 'reservationsId', visible: false, align: 'center', valign: 'middle'},
        {title: '病历号', field: 'medicalRecordNumber', align: 'center', valign: 'middle', sortable: true, width: 130},
        {
            title: '客户姓名', field: 'customerName', align: 'center', valign: 'middle', sortable: true, width: 120,
            formatter: function (value, row, index) {
                if (row.medicalRecordNumber == '合计') {
                    return value;
                } else {
                    if (row.customerAge == "" || row.customerAge == null || row.customerAge == "null") {
                        return "<a href='javascript:;' style='display: flex;align-items: center;'><img src='https://pynnn.oss-cn-hangzhou.aliyuncs.com/unkown-sex.png' alt='' width='40px'><span class='portraitHeadName'>" + value + "</span></a>"
                    } else if (row.customerAge >= 0 && row.customerAge < 18) {
                        if (row.customerSex == 1) {
                            return "<a href='javascript:;' style='display: flex;align-items: center;'><img src='https://pynnn.oss-cn-hangzhou.aliyuncs.com/nan-child.png' alt='' width='40px'><span class='portraitHeadName'>" + value + "</span></a>"
                        } else if (row.customerSex == 2) {
                            return "<a href='javascript:;' style='display: flex;align-items: center;'><img src='https://pynnn.oss-cn-hangzhou.aliyuncs.com/nv-child.png' alt='' width='40px'><span class='portraitHeadName'>" + value + "</span></a>"
                        } else {
                            return "<a href='javascript:;' style='display: flex;align-items: center;'><img src='https://pynnn.oss-cn-hangzhou.aliyuncs.com/unkown-sex.png' alt='' width='40px'><span class='portraitHeadName'>" + value + "</span></a>"
                        }
                    } else if (row.customerAge >= 18 && row.customerAge <= 50) {
                        if (row.customerSex == 1) {
                            return "<a href='javascript:;' style='display: flex;align-items: center;'><img src='https://pynnn.oss-cn-hangzhou.aliyuncs.com/female-adult.png' alt='' width='40px'><span class='portraitHeadName'>" + value + "</span></a>"
                        } else if (row.customerSex == 2) {
                            return "<a href='javascript:;' style='display: flex;align-items: center;'><img src='https://pynnn.oss-cn-hangzhou.aliyuncs.com/lfemale-child.png' alt='' width='40px'><span class='portraitHeadName'>" + value + "</span></a>"
                        } else {
                            return "<a href='javascript:;' style='display: flex;align-items: center;'><img src='https://pynnn.oss-cn-hangzhou.aliyuncs.com/unkown-sex.png' alt='' width='40px'><span class='portraitHeadName'>" + value + "</span></a>"
                        }
                    } else if (row.customerAge > 50) {
                        if (row.customerSex == 1) {
                            return "<a href='javascript:;' style='display: flex;align-items: center;'><img src='https://pynnn.oss-cn-hangzhou.aliyuncs.com/male-old.png' alt='' width='40px'><span class='portraitHeadName'>" + value + "</span></a>"
                        } else if (row.customerSex == 2) {
                            return "<a href='javascript:;' style='display: flex;align-items: center;'><img src='https://pynnn.oss-cn-hangzhou.aliyuncs.com/female-old.png' alt='' width='40px'><span class='portraitHeadName'>" + value + "</span></a>"
                        } else {
                            return "<a href='javascript:;' style='display: flex;align-items: center;'><img src='https://pynnn.oss-cn-hangzhou.aliyuncs.com/unkown-sex.png' alt='' width='40px'><span class='portraitHeadName'>" + value + "</span></a>"
                        }
                    }
                }
            }
        },
        {title: '门店', field: 'arrivalStore', align: 'center', valign: 'middle', sortable: true, width: 100},
        {title: '医生', field: 'doctor', align: 'center', valign: 'middle', sortable: true, width: 80,},
        {title: 'TC', field: 'fieldTc', align: 'center', valign: 'middle', sortable: true, width: 80,},
        {
            title: '性别', field: 'customerSex', align: 'center', valign: 'middle', sortable: true, width: 80,
            formatter: function (value, row, index) {
                if (row.medicalRecordNumber == '合计') {
                    return value;
                } else {
                    if (value == 1) {
                        return "男";
                    } else if (value == 2) {
                        return "女";
                    } else {
                        return "未知";
                    }
                }
            }
        },
        {
            title: '年龄', field: 'customerAge', align: 'center', valign: 'middle', sortable: true, width: 80,
            formatter: function (value, row, index) {
                if (row.medicalRecordNumber == '合计') {
                    return null;
                } else {
                    return value;
                }
            }
        },
        {title: '执行时间', field: 'createTime', align: 'center', valign: 'middle', sortable: true, width: 170,},
        {title: '科室', field: 'treatmentUnit', align: 'center', valign: 'middle', sortable: true, width: 80,},
        {title: '项目', field: 'appointmentPayService', align: 'center', valign: 'middle', sortable: true, width: 100,},
        {
            title: '数量', field: 'number', align: 'center', valign: 'middle', sortable: true, width: 80,
            formatter: function (value, row, index) {
                if (row.medicalRecordNumber == '合计') {
                    return null;
                } else {
                    return value;
                }
            }
        },
        {title: '执行业绩', field: 'serviceActualPayS', align: 'center', valign: 'middle', sortable: true, width: 110,},
        {title: '分诊业绩', field: 'totalServiceActualPay', align: 'center', valign: 'middle', sortable: true, width: 100,},
    ]
    return columns;
};

Performance.search = function () {
    Performance.table.refresh({query: Performance.formParams()});
    Performance.total();
    Performance.totals();
};

Performance.formParams = function () {
    var queryData = {};
    queryData['reach'] = $("#doctorInputPublic", parent.document).val();
    var valueArrivalStore = $('#arrivalStore').val();
    var strArrivalStore = "";
    if (valueArrivalStore != null && valueArrivalStore != "") {
        for (var i = 0; i < valueArrivalStore.length; i++) {
            if (i == (valueArrivalStore.length - 1)) {
                strArrivalStore += valueArrivalStore[i];
            } else {
                strArrivalStore += valueArrivalStore[i] + ",";
            }
        }
    }
    queryData['arrivalStore'] = strArrivalStore;  //门店
    queryData['treatmentUnit'] = $("#treatmentUnit").val();
    var valueAppointmentPayService = $('#appointmentPayService').val();
    var strAppointmentPayService = "";
    if (valueAppointmentPayService != null && valueAppointmentPayService != "") {
        for (var i = 0; i < valueAppointmentPayService.length; i++) {
            if (i == (valueAppointmentPayService.length - 1)) {
                strAppointmentPayService += valueAppointmentPayService[i];
            } else {
                strAppointmentPayService += valueAppointmentPayService[i] + ",";
            }
        }
    }
    queryData['appointmentPayService'] = strAppointmentPayService;
    queryData['fieldTc'] = $("#fieldTc").val();
    queryData['startCreateTime'] = $("#startCreateTime").val();
    queryData['endCreateTime'] = $("#endCreateTime").val();
    queryData['doctor'] = $("#doctor").val();
    queryData['dimensionSelection'] = $("#dimensionSelection").val();
    return queryData;
};

Performance.total = function () {
    var param3 = $("#arrivalStore").val();
    var strArrivalStore = "";
    if (param3 != null && param3 != "") {
        for (var i = 0; i < param3.length; i++) {
            if (i == (param3.length - 1)) {
                strArrivalStore += param3[i];
            } else {
                strArrivalStore += param3[i] + ",";
            }
        }
    }
    param3 = strArrivalStore;
    var param4 = $("#appointmentPayService").val();
    var strAppointmentPayService = "";
    if (param4 != null && param4 != "") {
        for (var i = 0; i < param4.length; i++) {
            if (i == (param4.length - 1)) {
                strAppointmentPayService += param4[i];
            } else {
                strAppointmentPayService += param4[i] + ",";
            }
        }
    }
    param4 = strAppointmentPayService;
    var param1 = $("#startCreateTime").val();  //预约到店时间
    var param2 = $("#endCreateTime").val();
    var doctor = $("#doctor").val();
    var reach = $("#doctorInputPublic", parent.document).val();
    var treatmentUnit = $("#treatmentUnit").val();
    var fieldTc = $("#fieldTc").val();
    $.ajax({
        type: "GET",
        url: Feng.ctxPath + "/doctorLooked/totalAccount",
        data: {
            param1: param1,
            param2: param2,
            param3: param3,
            param4: param4,
            treatmentUnit: treatmentUnit,
            fieldTc: fieldTc,
            doctor: doctor,
            reach: reach
        },
        success: function (info) {
            if (info == "" || info == null || info == undefined) {
                $("#all").html(0);//总金额
            } else {
                $("#all").html(info.totalPays);//总金额
            }
        }
    });
};

Performance.totals = function () {
    var param3 = $("#arrivalStore").val();
    var strArrivalStore = "";
    if (param3 != null && param3 != "") {
        for (var i = 0; i < param3.length; i++) {
            if (i == (param3.length - 1)) {
                strArrivalStore += param3[i];
            } else {
                strArrivalStore += param3[i] + ",";
            }
        }
    }
    param3 = strArrivalStore;
    var param4 = $("#appointmentPayService").val();
    var strAppointmentPayService = "";
    if (param4 != null && param4 != "") {
        for (var i = 0; i < param4.length; i++) {
            if (i == (param4.length - 1)) {
                strAppointmentPayService += param4[i];
            } else {
                strAppointmentPayService += param4[i] + ",";
            }
        }
    }
    param4 = strAppointmentPayService;
    var param1 = $("#startCreateTime").val();  //预约到店时间
    var param2 = $("#endCreateTime").val();
    var doctor = $("#doctor").val();
    var reach = $("#doctorInputPublic", parent.document).val();
    var treatmentUnit = $("#treatmentUnit").val();
    var fieldTc = $("#fieldTc").val();
    $.ajax({
        type: "GET",
        url: Feng.ctxPath + "/doctorLooked/totalAccountFen",
        data: {
            param1: param1,
            param2: param2,
            param3: param3,
            param4: param4,
            treatmentUnit: treatmentUnit,
            fieldTc: fieldTc,
            doctor: doctor,
            reach: reach
        },
        success: function (data) {
            if (data == "" || data == null || data == undefined) {
                $("#fall").html(0);//总金额
            } else {
                $("#fall").html(data.totalfoldingPayS);//总金额
            }
        }
    });
};

Performance.resetSearch = function () {
    $("#doctor").val("");
    $("#treatmentUnit").val("");
    $("#arrivalStore").selectpicker('deselectAll');
    $("#appointmentPayService").selectpicker('deselectAll');
    $("#fieldTc").val("");
    $("#startCreateTime").val("");
    $("#endCreateTime").val("");
    Performance.search();
};

Performance.downLoad = function () {
    var queryData = {};
    queryData['reach'] = $("#doctorInputPublic", parent.document).val();
    queryData['treatmentUnit'] = $("#treatmentUnit").val();
    queryData['fieldTc'] = $("#fieldTc").val();
    queryData['startCreateTime'] = $("#startCreateTime").val();
    queryData['endCreateTime'] = $("#endCreateTime").val();
    queryData['doctor'] = $("#doctor").val();
    queryData['dimensionSelection'] = $("#dimensionSelection").val();
    var value = $('#appointmentPayService').val();
    var str = "";
    if (value != null && value != "") {
        for (var i = 0; i < value.length; i++) {
            if (i == (value.length - 1)) {
                str += value[i];
            } else {
                str += value[i] + ",";
            }
        }
    }
    var value = $('#arrivalStore').val();
    var str = "";
    if (value != null && value != "") {
        for (var i = 0; i < value.length; i++) {
            if (i == (value.length - 1)) {
                str += value[i];
            } else {
                str += value[i] + ",";
            }
        }
    }
    queryData['arrivalStore'] = str;
    var param = parseParam(queryData);
    location.href = Feng.ctxPath + '/doctorLooked/doctorExport?' + param;
};

var parseParam = function (param) {//把对象拼接成URL的键值对参数param1=value1&param2=value2
    var paramStr = "";
    $.each(param, function (i) {
        paramStr += '&' + getSingleData(param, i);
    });
    return paramStr.substr(1);
};
var getSingleData = function (param, key) {
    return key + "=" + param[key];
};

Performance.executivePerformance = function (obj) {
    $("#dimensionSelection").val(obj);
    Performance.search()
};

Performance.requestList = function () {
    var seleteDoctorValue = $('#doctorValue').val();
    if (seleteDoctorValue != "") {
        $("#doctor option[value='" + seleteDoctorValue + "']").attr("selected", "selected");
    }
    var seletetcName = $('#tcNameValue').val();
    if (seletetcName != "") {
        $("#tcName option[value='" + seletetcName + "']").attr("selected", "selected");
    }
};

Performance.naturalDaySearch = function (obj) {
    if (obj == 1) {
        $("#startCreateTime").val(setDateTime(new Date(), 0) + " " + "00:00:00");
        $("#endCreateTime").val(setDateTime(new Date(), 0) + " " + "23:59:59");
        $(".timeBox > div button").eq(0).addClass('actionClick').siblings().removeClass('actionClick');
    } else if (obj == 2) {
        var AccountsTime = $("#sttleAccountsTime").val();
        $("#startCreateTime").val(setDateTime(new Date(), 1) + " " + AccountsTime);
        $("#endCreateTime").val(setDateTime(new Date(), 0) + " " + AccountsTime);
        $(".timeBox > div button").eq(1).addClass('actionClick').siblings().removeClass('actionClick');
    }
    Performance.search();
};

$("#doctorSearchAll", parent.document).click(function () {
    Performance.search();
});

$(function () {
    var defaultColunms = Performance.initColumn();
    var table = new BSTable(Performance.id, "/doctorLooked/queryList", defaultColunms);
    table.setPaginationType("server");
    table.setQueryParams(Performance.formParams());
    Performance.table = table.init();
    Performance.naturalDaySearch(1);
    Performance.requestList();
    menuNameHideColumn($("#menuName").val(),'CustomerPerformanceTable');
    checkDate = function () {Performance.search();};
    $(document).on('click', '#laydate_clear,#laydate_today,#laydate_ok', function () {Performance.search();});
});